/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2005  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/


/******************************************************************/
/* Includes                                                       */
/******************************************************************/

// module's interface
#include "AskDlg.h"

// project's headers
#include "Language.h"
#include "Resources.h"
#include "Rule.h"


/******************************************************************/
/* Internal constants                                             */
/******************************************************************/

// time-out timer id
#define TIMER_TIMEOUT	1

#define TIMEOUT_PERIOD	40 /*seconds*/

// color key used as transparent color
#define TRANSPARENT_COLOR	RGB(252,191,211)


/******************************************************************/
/* Internal data types                                            */
/******************************************************************/

typedef struct {
  UINT	nTimeOut ;
  UINT	nDefReaction ;
} WNDDATA ;


/******************************************************************/
/* Exported function : DlgProc                                    */
/******************************************************************/

BOOL CALLBACK AskDlg_DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{
  static	HINSTANCE	g_hInstance ;
  static	HBITMAP		g_hbmpPicture ;

  WNDDATA	* pData ;
    
  TCHAR		szBuffer[256] ;
  RECT		rect ;
  int		x, y ;
  HDC		hdc ;
  PAINTSTRUCT	ps ;
  BITMAP	bmpInfo ;
  int		nResult ;
  HDC		hdcPicture ;
 

  pData = (WNDDATA*) GetWindowLong (hDlg, GWL_USERDATA) ;

  switch( message )
    {
    case WM_INITDIALOG:

      // get instance handle
      g_hInstance = (HINSTANCE) GetWindowLong (hDlg, GWL_HINSTANCE) ;

      pData = (WNDDATA*) malloc (sizeof(WNDDATA)) ;
      SetWindowLong (hDlg, GWL_USERDATA, (LONG)pData) ;

      pData->nTimeOut = TIMEOUT_PERIOD ;
      SendMessage (hDlg, WM_TIMER, TIMER_TIMEOUT, 0) ;
      SetTimer (hDlg, TIMER_TIMEOUT, 1000, NULL) ;     

      pData->nDefReaction = ((ASKDLGPARAMS*)lParam)->nDefReaction ;      

      // load picture
      if( ! g_hbmpPicture )
	g_hbmpPicture = LoadImage (g_hInstance, MAKEINTRESOURCE(IDB_ASK), 
				   IMAGE_BITMAP, 0, 0, 
				   LR_CREATEDIBSECTION) ;
      
      wsprintf (szBuffer, TEXT("%s (%u)"), 
		((ASKDLGPARAMS*)lParam)->szProcess,
		((ASKDLGPARAMS*)lParam)->dwProcessID) ;
      SetDlgItemText (hDlg, IDC_PROCESS, szBuffer) ;
      SendDlgItemMessage (hDlg, IDC_PROCESS, EM_SETSEL, 0, 0) ;      

      Condition_ToString (((ASKDLGPARAMS*)lParam)->pCondition, szBuffer, 256) ;
      SetDlgItemText (hDlg, IDC_REASON, szBuffer) ;
      SendDlgItemMessage (hDlg, IDC_REASON, EM_SETSEL, 0, 0) ;    

      if( Language_IsLoaded() ) {
	SetDlgItemText (hDlg, IDT_PROCESS, STR(_THE_FOLLOWING_PROCESS)) ;
	SetDlgItemText (hDlg, IDT_REASON, STR(_IS_TRYING_TO)) ;
	SetDlgItemText (hDlg, IDT_NEW_FILTER, STR(_YOU_CAN_CREATE_A_FILTER)) ;
	SetDlgItemText (hDlg, IDC_NEW_FILTER, STR(_NEW_FILTER)) ;
	SetDlgItemText (hDlg, IDC_ACCEPT, STR(_ACCEPT)) ;
	SetDlgItemText (hDlg, IDC_FEIGN, STR(_FEIGN)) ;
	SetDlgItemText (hDlg, IDC_REJECT, STR(_REJECT)) ;
      }	  
      
      SetFocus (hDlg) ;
      SetForegroundWindow (hDlg) ;

      return TRUE ;



    case WM_DESTROY:

      KillTimer (hDlg, TIMER_TIMEOUT) ;
      free (pData) ;

      return TRUE ;

    case WM_PAINT:

      // get window size
      GetClientRect (hDlg, &rect) ;
      
      // get image size
      GetObject (g_hbmpPicture, sizeof(BITMAP), &bmpInfo) ;

      y = ( rect.bottom - bmpInfo.bmHeight ) >> 1 ;
      x = rect.right - bmpInfo.bmWidth - y ;

      
      hdc = BeginPaint (hDlg, &ps) ;

      hdcPicture = CreateCompatibleDC (hdc) ;
      SelectObject (hdcPicture, g_hbmpPicture) ;  

      TransparentBlt (hdc, 
		      x, y,
		      bmpInfo.bmWidth, bmpInfo.bmHeight,
		      hdcPicture,
		      0, 0,
		      bmpInfo.bmWidth, bmpInfo.bmHeight,
		      TRANSPARENT_COLOR) ;

      DeleteDC (hdcPicture) ;

      EndPaint (hDlg, &ps) ;

      return FALSE ;
 
    case WM_TIMER:
      
      wsprintf (szBuffer, TEXT("What do you want ? (%s in %d seconds)"), 
		pData->nDefReaction==RULE_REJECT ? TEXT("reject") :
		pData->nDefReaction==RULE_FEIGN ? TEXT("feign") : TEXT("accept"),
		pData->nTimeOut) ;
      SetDlgItemText (hDlg, IDT_REACTION, szBuffer) ;
		
      if( ! --pData->nTimeOut )
	{
	  
	  PostMessage (hDlg, WM_COMMAND, 
		       pData->nDefReaction==RULE_REJECT ? IDC_REJECT :
		       pData->nDefReaction==RULE_FEIGN ? IDC_FEIGN : 
		       IDC_ACCEPT, 0) ;
	}

      return TRUE ;


    case WM_COMMAND:

      switch( LOWORD(wParam) )
	{
	case IDC_NEW_FILTER:

	  EndDialog (hDlg, ASKDLG_NEW_FILTER) ;
	  
	  return TRUE ;

	case IDC_ACCEPT:	  
	case IDC_FEIGN:
	case IDC_REJECT:
	  
	  nResult = RULE_ACCEPT ;
	  if( LOWORD(wParam)==IDC_FEIGN ) nResult = RULE_FEIGN ;
	  if( LOWORD(wParam)==IDC_REJECT ) nResult = RULE_REJECT ; 
		  	  
	  EndDialog (hDlg, nResult) ;
	  return TRUE ;
	}

      return FALSE ; // case WM_COMMAND:
    }
 
  return FALSE ;
}
